<?php

    fta_registerModules(
        array(
            "Services/oEmbed",
            "Services/oEmbed/Object/Video"
        )
   );

    use \Services_oEmbed;
    use \Services_oEmbed_Object_Video;

    class FTAOEmbed {

        protected $videoUrl;
        protected $oEmbedUrl;
        protected $options;

        function __construct($videoUrl, $oEmbedUrl,$options=array()){
            $this->videoUrl = $videoUrl;
            $this->oEmbedUrl = $oEmbedUrl;
        }


        function getEmbed($width, $height){

            //make a key that is not ugly.
            $key = new stdClass();
            $key->width = $width;
            $key->height = $height;
            $key->host = CMS_HTTP_HOST;
            $key->videoUrl = $this->videoUrl;

            $key = json_encode($key);

            $embed = apc_fetch($key);
            if ($embed) {
                return $embed;
            }

            $options = array();

            $oEmbed = new \Services_oEmbed(
                $this->videoUrl,
                array_merge(
                    array(\Services_oEmbed::OPTION_API=> $this->oEmbedUrl, \Services_oEmbed::OPTION_TIMEOUT=>15),
                    $options
                )
            );

            if (!$oEmbed){
                return null;
            }

            $embed = $oEmbed->getObject(array('width'=>$width, 'height'=>$height ));

            apc_store($key, $embed );// add ttl// here or cache for ever?

            return $embed;

        }



    }